<?php

!defined('IN_ROOT') && exit('Access Denied');

class admin_areacontrol extends base {

    function admin_areacontrol(& $get,& $post) {
        $this->base( & $get,& $post);
        $this->load('area');
    }

    function ondefault() {
        $area['grade'] = $pid =0;
        $arealist = $_ENV['area']->list_by_pid($pid);
        include template('arealist','admin');
    }

    function onadd() {
        if(isset($this->post['submit'])) {
            $pid = $this->post['pid'] ;
            $lines = explode("\n" , $this->post['areas']);
            $_ENV['area']->add($lines,$pid);
            cleardir(MY_ROOT.'/data/cache'); //清除缓存文件
            $this->ondefault();
        }else {
            $id=intval($this->get[2]);
            $selectedarray=array();
            if($id) {
                $area = $this->area[$id];
                $item=$area;
                for($grade=$area['grade'];$grade>0;$grade--) {
                    $selectedarray[]=$item['id'];
                    $item['pid'] && $item=$this->area[$item['pid']];
                }
            }
            $selected_id_list=implode(' ',array_reverse($selectedarray));
            $area_js=$_ENV['area']->select_option_cate($id);
            include template('addarea','admin');
        }
    }

    function onedit() {
        $id = (isset($this->get[2]))?$this->get[2]:$this->post['id'];
        if(isset($this->post['submit'])) {
            $name=trim($this->post['name']);
            $areadir=trim($this->post['dir']);
            $areadir=empty($areadir)?getpinyin($name):$areadir;
            $cid=$this->post['cid'];
            empty($cid) && $cid=0;
            $_ENV['area']->update_by_id($id,$name,$areadir,$cid);
            cleardir(MY_ROOT.'/data/cache'); //清除缓存文件
            $this->post['cid']? $this->onview($this->post['cid']) :$this->ondefault();
        }else {
            $area = $this->area[$id];
            $item=$area;
            $selectedarray=array();
            for($grade=$area['grade'];$grade>1;$grade--) {
                $selectedarray[]=$item['pid'];
                $item=$this->area[$item['pid']];
            }
            $selected_id_list=implode(' ',array_reverse($selectedarray));
            $area_js=$_ENV['area']->select_option_cate($area[pid]);
            include template('editarea','admin');
        }
    }

    //后台分类管理查看一个分类
    function onview($cid=0,$msg='') {
        $cid = $cid ? $cid : intval($this->get[2]);
        $navlist=$_ENV['area']->get_navigation($cid);//获取导航
        $area = $this->area[$cid];
        $arealist=$_ENV['area']->list_by_cid_pid($cid,$area['pid']);//获取子分类
        $pid = $cid;
        $msg && $message=$msg;
        include template('arealist','admin');
    }

    //删除分类
    function onremove() {
        if(isset($this->post['cid'])) {
            $cids = implode(",",$this->post['cid']);
            $pid = intval($this->post['hiddencid']);
            $_ENV['area']->remove($cids);
            $this->onview($pid,'分类删除成功!');
        }
    }

    /*后台分类排序*/
    function onreorder() {
        $orders = explode(",",$this->post['order']);
        $hid = intval($this->post['hiddencid']);
        foreach($orders as $order => $cid) {
            $_ENV['area']->order_area(intval($cid),$order);
        }
        $this->cache->remove('area');
    }
}
?>